﻿using System;
using System.Data;

namespace Smatime.Web.Models
{
    public class UserHistory : BaseEntity
    {
        public string UserAccessId { get; set; }
        public DateTime? LoginTime { get; set; }
        public DateTime? LogoutTime { get; set; }
        public string SourceIp { get; set; }
        public string HostName { get; set; }
        public string Content { get; set; }
        public string EditTarget { get; set; }
        public string EditContent { get; set; }
        public string BeforeEdit { get; set; }
        public string AfterEdit { get; set; }

        public override BaseEntity FromDataRow(DataRow row)
        {
            Id = (long) row["Id"];
            UserAccessId = row["UserAccessId"].ToString();
            LoginTime = row["LoginTime"].ToDateTime();
            LogoutTime = row["LogoutTime"].ToDateTime();
            SourceIp = row["SourceIp"].ToString();
            HostName = row["HostName"].ToString();
            Content = row["Content"].ToString();
            EditTarget = row["EditTarget"].ToString();
            EditContent = row["EditContent"].ToString();
            BeforeEdit = row["BeforeEdit"].ToString();
            AfterEdit = row["AfterEdit"].ToString();
            return base.FromDataRow(row);
        }

        public override void ToDataRow(DataRow row)
        {
            row["UserAccessId"] = UserAccessId;
            row["LoginTime"] = LoginTime;

            //TA : 2014/09 convert null to DBNul
            if (LogoutTime == null)
            {
                row["LogoutTime"] = DBNull.Value;
            }
            else
            {
                row["LogoutTime"] = LogoutTime;
            }
            //END

            row["SourceIp"] = SourceIp;
            row["HostName"] = HostName;
            row["Content"] = Content;
            row["EditTarget"] = EditTarget;
            row["EditContent"] = EditContent;
            row["BeforeEdit"] = BeforeEdit;
            row["AfterEdit"] = AfterEdit;

            base.ToDataRow(row);
        }
    }
}